//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_dao

import (
	"context"
	"time"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_member_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type BasicRoleInfoDbItem struct {
	RoleName string `bson:"roleName"`
	Admin    bool   `bson:"admin"`
}

type RoleInfoDbItem struct {
	RoleId      string              `bson:"_id"`
	ProjectId   string              `bson:"projectId"`
	BasicInfo   BasicRoleInfoDbItem `bson:"basicInfo"`
	CreateTime  int64               `bson:"createTime"`
	UpdateTime  int64               `bson:"updateTime"`
	DefaultRole bool                `bson:"defaultRole"`
}

func (item *RoleInfoDbItem) ToRoleInfo() *project_member_api.RoleInfo {
	return &project_member_api.RoleInfo{
		RoleId:    item.RoleId,
		ProjectId: item.ProjectId,
		BasicInfo: &project_member_api.BasicRoleInfo{
			RoleName: item.BasicInfo.RoleName,
			Admin:    item.BasicInfo.Admin,
		},
		CreateTime:  item.CreateTime,
		UpdateTime:  item.UpdateTime,
		DefaultRole: item.DefaultRole,
	}
}

type _MemberRoleDao struct {
	mc *mongo.Collection
}

func newMemberRoleDao(db *mongo.Database) (*_MemberRoleDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT + "member_role")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_MemberRoleDao{
		mc: mc,
	}, nil
}

func (db *_MemberRoleDao) Insert(ctx context.Context, dbItem *RoleInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_MemberRoleDao) Get(ctx context.Context, projectId, roleId string) (*RoleInfoDbItem, error) {
	retItem := &RoleInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"projectId": projectId, "_id": roleId}).Decode(retItem)
	return retItem, err
}

func (db *_MemberRoleDao) UpdateBasicInfo(ctx context.Context, projectId, roleId string, basicItem *BasicRoleInfoDbItem) error {
	nowTime := time.Now().UnixNano() / 1e6
	_, err := db.mc.UpdateOne(ctx, bson.M{"projectId": projectId, "_id": roleId}, bson.M{"$set": bson.M{"basicInfo": basicItem, "updateTime": nowTime}})
	return err
}

func (db *_MemberRoleDao) ListByProjectId(ctx context.Context, projectId string) ([]*RoleInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId})
	if err != nil {
		return nil, err
	}
	retList := []*RoleInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_MemberRoleDao) ListByProjectIdList(ctx context.Context, projectIdList []string) ([]*RoleInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": bson.M{"$in": projectIdList}})
	if err != nil {
		return nil, err
	}
	retList := []*RoleInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_MemberRoleDao) ListById(ctx context.Context, projectIdList, roleIdList []string) ([]*RoleInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": bson.M{"$in": projectIdList}, "_id": bson.M{"$in": roleIdList}})
	if err != nil {
		return nil, err
	}
	retList := []*RoleInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_MemberRoleDao) Exist(ctx context.Context, projectId, roleId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"projectId": projectId, "_id": roleId})
	if err != nil {
		return false, err
	}
	return count > 0, nil
}

func (db *_MemberRoleDao) Remove(ctx context.Context, projectId, roleId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"projectId": projectId, "_id": roleId})
	return err
}
